package org.donhuan.rms.ui.controls;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;

public class CompositeTextToolbar extends Composite {
	private Text text;
	private int offset = 2;
	private ToolBar toolBar;

	public CompositeTextToolbar(Composite parent, int style) {
		super(parent, style);
		setLayout(new FormLayout());

		toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		text = new Text(this, SWT.BORDER);
		toolBar.setBackground(text.getBackground());
		FormData fd_text = new FormData();
		fd_text.top = new FormAttachment(0);
		fd_text.right = new FormAttachment(100);
		fd_text.left = new FormAttachment(0);
		fd_text.bottom = new FormAttachment(100);
		text.setLayoutData(fd_text);
		FormData fd_toolBar = new FormData();
		fd_toolBar.top = new FormAttachment(0, offset);
		fd_toolBar.right = new FormAttachment(100, -offset);
		// fd_toolBar.left = new FormAttachment(0);
		fd_toolBar.bottom = new FormAttachment(100, -offset);
		toolBar.setLayoutData(fd_toolBar);

	}

	public void addActionItem(Action... actions) {
		if (actions != null) {
			IToolBarManager toolBarManager = new ToolBarManager(getToolBar());
			toolBarManager.removeAll();
			for (int i = 0; i < actions.length; i++) {
				toolBarManager.add(actions[i]);
			}
			toolBarManager.update(true);
		}
	}

	public Text getText() {
		return text;
	}

	public void setText(Text text) {
		this.text = text;
	}

	public ToolBar getToolBar() {
		return toolBar;
	}

	public void setToolBar(ToolBar toolBar) {
		this.toolBar = toolBar;
	}
}
